import 'package:collection/collection.dart';
import 'package:flutter_axrjc/datasource/network/api/QianDaoApi.dart';
import 'package:flutter_axrjc/datasource/network/api/TaskApi.dart';
import 'package:flutter_axrjc/datasource/network/api/UserApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/EventBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/QianDaoListBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/QianDaoResBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/TaskBean.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_axrjc/main.dart';
import 'package:flutter_axrjc/util/Util.dart';
import 'package:flutter_axrjc/widget/dialog.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'state.dart';

class AttendanceLogic extends GetxController {
  final AttendanceState state = AttendanceState();

  @override
  void onInit() {
    super.onInit();
    getQianDao();
    loadUser();
    refreshUser();
    getTaskList();
  }

  Future<void> loadUser() async {
    state.user.value = await UserApi.getUser();
  }

// todo
//   Future<void> qiandao() async {
//     if (state.qianDaoList.value != null) {
//       try {
//         await QianDaoApi.qiandao(state.qianDaoList.value!);
//         await refreshUser();
//         showQiaoDao(isFinish: false, bean: state.qianDaoList.value!); //显示签到成功
//       } catch (e) {
//         print("ApiException");
//         showQiaoDao(isFinish: true, bean: state.qianDaoList.value!);
//       }
//     } else {
//       EasyLoading.showToast("获取签到数据失败");
//     }
//   }

  Future<void> getQianDao() async {
    try {
      state.qianDaoList.clear();
      state.qianDaoList.addAll(await QianDaoApi.proSysCodeQuery());
      state.day.value =
          "${state.qianDaoList.lastWhereOrNull((element) => element.flag == "1")?.day ?? 0}";
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      print(e);
    }
  }

  refreshUser() async {
    try {
      var user = await UserApi.getUser();
      user?.customerInfo = await UserApi.getCustomerInfo();
      state.userIntegral.value = "${user?.customerInfo?.userIntegral ?? 0}";
      await Util.saveUser(user);
      eventBus.fire(EventBean(type: EventType.refreshProfile, data: null));
    } catch (e) {
      print("刷新用户数据");
    }
  }

  Future<void> getTaskList() async {
    state.taskList.clear();
    try {
      state.taskList.addAll(await TaskApi.getList());
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      print(e);
    }
  }

  void jump(TaskBean bean) {
    if (bean.ifFinish == "0") {
      Get.back(result: bean.taskType);
    } else {
      EasyLoading.showToast("该任务已完成");
    }
  }

  Future<void> qiandao(QianDaoListBean bean) async {
    try {
      QianDaoResBean? res = await QianDaoApi.qiandao("${bean.pointsNumber}");
      await refreshUser();
      showQiaoDao(res, "${bean.pointsNumber}", state.day.value);
      getQianDao();
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
      // showQiaoDao(isFinish: true, bean: "${bean.day}");
    }
  }
}
